const {
  createHash
} = require('crypto');
const _ = require('lodash');
const handleFileType = require('./handleFileType')
const handleSimilarity = require('./handleSimilarity')


function parseData(data, options) {
  const res = {} 
  let commitCount = data.length
  let commitLineCount = 0
  data.forEach(commit => {
    //  deep copy commit 
    let _commit = _.cloneDeep(commit)
    const authorID = createHash('sha256').update(_commit.author['alias'] + _commit.author['email']).digest('hex')
    if(!res[authorID]){
      res[authorID] = {
        ..._commit.author,
        commits: [],
        commitLine: 0,
      }
    }

    if(options.fileTypes && options.fileTypes.length > 0){
      _commit = handleFileType(commit, options.fileTypes)   
    } 
    
    if(options.hasDiff && options.similarity > 0){
      _commit = handleSimilarity(commit, options.similarity)   
    } 

    const commitLine = _commit.insertions - _commit.deletions
    commitLineCount += commitLine
    res[authorID].commitLine += commitLine
    res[authorID].commits.push(_commit.sha)   
  })
  return [commitCount, commitLineCount, res]
}

module.exports = parseData